﻿using System.Linq.Extensions;
using System.Linq.PageList;
using Dyao.Book.Contract;
using Dyao.Book.Entity;
using Dyao.Book.Repository.Abstractions;
using Foundation.Core;

namespace Dyao.Book.Repository
{
    using Foundation.Replacement;

    public class BookMgrRepository : DataRepository<BookEntity, string>, IBookMgrRepository
    {
        /// <inheritdoc cref="IBookMgrRepository.GetPageListAsync"/>
        public async Task<IPageList<BookEntity>> GetPageListAsync(IPageFilter<BookFilterDto> pageFilter, CancellationToken cancellationToken = default)
        {
            var filter = pageFilter.Filter ?? new BookFilterDto();
            var query = this.GetQueryable()
                .WhereIf(b => b.Title.Contains(filter.Title), !filter.Title.IsNullOrEmpty())
                .WhereIf(b => b.Author == filter.Author, !filter.Author.IsNullOrEmpty())
                .WhereIf(b => b.SourceType == filter.SourceType, filter.SourceType.HasValue)
                .OrderOrThenBy(x => x.CreateDateTime, false)
                .AsNoTracking();

            return await query.ToPageListAsync(pageFilter, cancellationToken);
        }


    }
}
